package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.sys.entity.CarOrderinfo;
import io.renren.modules.sys.dao.CarOrderinfoMapper;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.CarOrderinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2022-09-05
 */
@Service
public class CarOrderinfoServiceImpl extends ServiceImpl<CarOrderinfoMapper, CarOrderinfo> implements CarOrderinfoService {

    @Override
    public PageUtils listAll(Map<String, Object> params) throws ParseException {

        String userName = (String) params.get("userName");
        String carCode = (String) params.get("carCode");
        String ready =  (String)params.get("ready");
        String commit = (String) params.get("commit");
        String status = (String) params.get("status");
        String statuses = (String) params.get("statuses");
        List<Integer> list = new ArrayList<>();
        list.add(Integer.valueOf(status));
        list.add(Integer.valueOf(statuses));
        Date ready1 = null;
        Date commit1 = null;
        if(!ready.equals(null) && !ready.equals("")) {
            ready1 = new SimpleDateFormat("yyyy-MM-dd").parse(ready);
             commit1 = new SimpleDateFormat("yyyy-MM-dd").parse(commit);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(ready1);
            calendar.add(Calendar.DATE, 1);
            ready1=calendar.getTime();

            calendar.setTime(commit1);
            calendar.add(Calendar.DATE, 1);
            commit1=calendar.getTime();
        }
         QueryWrapper <CarOrderinfo> queryWrapper =new QueryWrapper<>();
         if(!carCode.equals(null) && !carCode.equals("")){
             queryWrapper.eq("car_code", carCode);
         }
        if(!userName.equals(null) && !userName.equals("")){
            queryWrapper.like("user_name", userName);
        }
        if(ready1!=null && commit1!= null){
            queryWrapper.ge("create_time",ready1);
            queryWrapper.le("create_time",commit1);
        }
        queryWrapper.eq("del_flag",0);
        queryWrapper.in("status",list);
        IPage<CarOrderinfo> page = this.page(new Query<CarOrderinfo>().getPage(params),
        queryWrapper
                       );
        return new PageUtils(page);
    }

    @Override
    public PageUtils listAll1(Map<String, Object> params) throws ParseException {
        String userName = (String) params.get("userName");
        String carCode = (String) params.get("carCode");
        String ready =  (String)params.get("ready");
        String commit = (String) params.get("commit");
        String status = (String) params.get("status");
        Date ready1 = null;
        Date commit1 = null;
        if(!ready.equals(null) && !ready.equals("")) {
            ready1 = new SimpleDateFormat("yyyy-MM-dd").parse(ready);
            commit1 = new SimpleDateFormat("yyyy-MM-dd").parse(commit);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(ready1);
            calendar.add(Calendar.DATE, 1);
            ready1=calendar.getTime();

            calendar.setTime(commit1);
            calendar.add(Calendar.DATE, 1);
            commit1=calendar.getTime();
        }
        QueryWrapper <CarOrderinfo> queryWrapper =new QueryWrapper<>();
        if(!carCode.equals(null) && !carCode.equals("")){
            queryWrapper.eq("car_code", carCode);
        }
        if(!userName.equals(null) && !userName.equals("")){
            queryWrapper.like("user_name", userName);
        }
        if(ready1!=null && commit1!= null){
            queryWrapper.ge("create_time",ready1);
            queryWrapper.le("create_time",commit1);
        }
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("status",status);
        IPage<CarOrderinfo> page = this.page(new Query<CarOrderinfo>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }
}
